var express = require('express');
var router = express.Router();

var mysql = require('mysql');
var connection = mysql.createConnection({
  //   // host: '192.168.1.194',
  //   // user: 'tyf',
  //   // password: 'tyf123',
  //   // database: 'orderdata'
  // host: '192.168.1.194',
  // port: 3306, // MySQL 默认端口为 3306
  // user: 'qwn',  //用户名
  // password: 'qwn123', //用户密码
  // database: 'orderdata',  //数据库
  // useConnectionPooling: true
  host: 'localhost',
  port: 3306, // MySQL 默认端口为 3306
  user: 'root',  //用户名
  password: 'woaini.1.2', //用户密码
  database: 'orderdata',  //数据库
  useConnectionPooling: true
});

connection.connect(() => {
  console.log('数据库已经连接');
});

router.get('/curriculum', function (req, res, next) {
  connection.query('SELECT * FROM reactteacherdata', function (error, results, fields) {
    if (error) throw error;
    res.send(results);
  })
});
// 修改上架下架状态
router.post('/setstatebar', function (req, res, next) {
  console.log(req.body);
  let { id, cid } = req.body
  connection.query('SELECT * FROM reactteacherdata', function (error, results, fields) {
    if (error) throw error;
    let item = results.find(item => {
      return item.id === Number(id)
    })

    let data = JSON.parse(item.teacherClassList).map(it => {
      if (it.cid === cid) {
        if (it.statebar === 0) {
          it.statebar = 1
        } else {
          it.statebar = 0
        }
      }
      return JSON.stringify(it)
    })
    let str = `UPDATE REACTTEACHERDATA set teacherClassList = '[${data}]' WHERE id = '${id}'`
    connection.query(str, function (error, results, fields) {
      if (error) throw error;

    })

  })

});

// 添加
router.post('/add', function (req, res, next) {
  // console.log(req.body);
  let data = req.body;
  console.log(data);
  connection.query('SELECT * FROM reactteacherdata', function (err, rows, fields) {
    if (err) throw err;
    let index = rows.findIndex((item) => {
      return item.id === data.id
    })
    if (index !== -1) {
      let str = JSON.parse(rows[index].teacherClassList)
      console.log(str);
      if (str.length === 0) {
        data.data.cid = data.id + '01'
      } else {
        data.data.cid = Number(str[str.length - 1].cid) + 1 + ''
      }
      str.push(data.data)
      const newData = str.map((item) => {
        return JSON.stringify(item)
      })
      JSON.stringify(newData)
      var sql = `UPDATE reactteacherdata set teacherClassList = '[${newData}]' WHERE id = '${rows[index].id}'`
      console.log(sql);
      connection.query(sql, function (error, results, fields) {
        if (error) throw error;
        console.log(results);
      })
    }
  })
});


// 删除
router.post('/removeitem', function (req, res, next) {
  let { id, cid } = req.body
  connection.query('SELECT * FROM reactteacherdata', function (err, rows, fields) {
    if (err) throw err;
    let index = rows.findIndex(item => {
      return item.id === Number(id)
    })
    let item = JSON.parse(rows[index].teacherClassList)
    item.forEach((it, index) => {
      if (Number(it.cid) === Number(cid)) {
        item.splice(index, 1)
      }
    })
    let data = JSON.stringify(item)
    let str = `UPDATE REACTTEACHERDATA set teacherClassList = '${data}' WHERE id = '${id}'`
    connection.query(str, function (error, results, fields) {
      if (error) throw error;

    })
  })
});

// 修改课程
router.post('/setTeacherClassItem', function (req, res, next) {
  let { id, cid, obj } = req.body
  connection.query('SELECT * FROM reactteacherdata', function (error, results, fields) {
    if (error) throw error;
    let item = results.find(item => {
      return Number(item.id) === Number(id)
    })
    let data = JSON.parse(item.teacherClassList)
    data = data.map(it => {
      if (Number(it.cid) === Number(cid)) {
        for (let key in obj) {
          it[key] = obj[key]
        }
      }
      return it
    })
    data = JSON.stringify(data)
    let str = `UPDATE REACTTEACHERDATA set teacherClassList = '${data}' WHERE id = '${id}'`
    connection.query(str, function (error, results, fields) {
      if (error) throw error;

    })

  })
});

// react请求
router.get('/reactteacherdata', function (req, res, next) {
  connection.query('SELECT * FROM reactteacherdata', function (error, results, fields) {
    if (error) throw error;
    res.send(results);
  })
});

// 小程序修改收藏状态
router.get('/setcollect', function (req, res, next) {

  connection.query('SELECT * FROM reactteacherdata', function (error, results, fields) {
    let item = results.find(item => {
      return item.id === Number(req.query.id)
    })




    let data = JSON.parse(item.teacherClassList).map(it => {
      if (it.cid === req.query.cid) {
        if (it.collect === 0) {
          it.collect = 1
        } else {
          it.collect = 0
        }
      }
      return JSON.stringify(it)
    })
    let str = `UPDATE REACTTEACHERDATA set teacherClassList = '[${data}]' WHERE id = '${req.query.id}'`
    console.log(str);
    connection.query(str, function (error, results, fields) {
      if (error) throw error;

    })

  })

})
// 添加购物车
router.post('/addcar', function (req, res, next) {
  // console.log(req.body);  //item
  let item = req.body.item
  let cid = req.body.item.cid
  connection.query('SELECT * FROM carlist', function (error, results, fields) {
    if (error) throw error;
    let index = results.findIndex((it) => {
      return it.cid === cid
    })
    if (index === -1) {
      item.checked = 1
      let item1 = JSON.stringify(item)
      connection.query(`insert into carlist(cid,item) values( '${cid}','${item1}')`, function (error, swer, fields) {
        if (error) throw error;
        console.log(swer);
        res.send(swer);
      })
    } else {
      let num = req.body.item.num
      let list = JSON.parse(results[index].item)
      list.num = num
      list = JSON.stringify(list)
      connection.query(`UPDATE carlist set item = '${list}' WHERE cid= ${results[index].cid}`, function (error, swer, fields) {
        if (error) throw error;
        console.log(swer);
        res.send(swer);
      })
    }
  })
});
// 获取购物车
router.get('/getcar', function (req, res, next) {
  connection.query('SELECT * FROM carlist', function (error, results, fields) {
    if (error) throw error;
    res.send(results);
  })
});

//小程序 
// 修改checked
router.post('/setCarList', function (req, res, next) {
  let { id, checked } = req.body
  connection.query('SELECT * FROM carlist', function (error, results, fields) {
    if (error) throw error;
    let obj = results.find(item => {
      return item.cid === id
    })
    console.log(checked);
    let data = JSON.parse(obj.item)
    data.checked = checked
    console.log(JSON.stringify(data));
    // console.log(obj.item);
    data = JSON.stringify(data)
    console.log(`UPDATE carlist set item = '${data}' WHERE cid = '${id}'`);
    let str = `UPDATE carlist set item = '${data}' WHERE cid = '${id}'`
    connection.query(str, function (error, results, fields) {
      if (error) throw error;
      res.send("更改")
    })
  })
});
// 修改全选
router.post('/setAllCheck', function (req, res, next) {
  let { shopCar } = req.body
  connection.query('SELECT * FROM carlist', function (error, results, fields) {
    if (error) throw error;
    console.log(shopCar);
  })
});

// 小程序修改收藏状态
router.get('/setcollect', function (req, res, next) {
  connection.query('SELECT * FROM reactteacherdata', function (error, results, fields) {
    if (error) throw error;
    let item = results.find(item => {
      return item.id === Number(req.query.id)
    })

    let data = JSON.parse(item.teacherClassList).map(it => {

      if (it.cid === req.query.cid) {
        if (it.collect === 0) {
          it.collect = 1
        } else {
          it.collect = 0
        }
      }
      return JSON.stringify(it)
    })
    let str = `UPDATE REACTTEACHERDATA set teacherClassList = '[${data}]' WHERE id = '${req.query.id}'`
    connection.query(str, function (error, results, fields) {
      if (error) throw error;
      res.send("修改成功")
    })
  })

})
// 添加购物车
router.post('/addcar', function (req, res, next) {
  let item = req.body.item
  let cid = req.body.item.cid
  connection.query('SELECT * FROM carlist', function (error, results, fields) {
    if (error) throw error;
    console.log(results);
    let index = results.findIndex((it) => {
      return it.cid === cid
    })
    if (index === -1) {
      item.checked = 1
      let item1 = JSON.stringify(item)
      connection.query(`insert into carlist(cid,item) values( '${cid}','${item1}')`, function (error, swer, fields) {
        if (error) throw error;

        res.send(swer);
      })
    } else {
      let num = req.body.item.num
      let list = JSON.parse(results[index].item)
      list.num = num
      list = JSON.stringify(list)
      connection.query(`UPDATE carlist set item = '${list}' WHERE cid= ${results[index].cid}`, function (error, swer, fields) {
        if (error) throw error;
        res.send(swer);
      })
    }
  })
});
// 获取购物车
router.get('/getcar', function (req, res, next) {
  connection.query('SELECT * FROM carlist', function (error, results, fields) {
    if (error) throw error;
    res.send(results);
  })
});


module.exports = router;

